package com.hsq.ndmall.order.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


import com.hsq.ndmall.mq.constant.OrderRabbitMQ;
import com.hsq.ndmall.mq.dto.OrderMsgDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import com.hsq.ndmall.order.entity.OrderEntity;
import com.hsq.ndmall.order.service.OrderService;
import com.hsq.ndmall.common.utils.PageUtils;
import com.hsq.ndmall.common.utils.R;
import io.swagger.annotations.Api;


/**
 * 订单表
 *
 * @author Mr.Huang
 * @email 1607922199@qq.com
 * @date 2021-08-19 15:01:33
 */
@Api(tags = "OrderController", description = "订单表服务")
@RestController
@RequestMapping("order/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("order:order:list")
    @ApiOperation("获取所有订单表信息")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("order:order:info")
    @ApiOperation("获取指定id的订单表详情")
    public R info(@PathVariable("id") Integer id) {
        OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加订单表信息")
    //@RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order) {
        boolean save = orderService.save(order);
        // TODO MQ发送消息到队列，查看订单的状态
        if (save) {
            OrderMsgDto orderMsgDto = new OrderMsgDto();
            orderMsgDto.setOrderId(order.getId());
            orderMsgDto.setOrderSn(order.getOrderSn());
            rabbitTemplate.convertAndSend(OrderRabbitMQ.ORDER_EVENT_EXCHANGE, OrderRabbitMQ.ORDER_CREATE_MSG, orderMsgDto);
            log.info("发生者发生消息：{}", orderMsgDto);
        }
        return R.ok().put("orderId", order.getId());
    }

    /**
     * TODO 老师的保存
     *
     * @param
     * @param
     * @return
     */
    @PostMapping(value = "/saveCopy", consumes = MediaType.APPLICATION_JSON_VALUE)
    //@RequiresPermissions("order:order:save")
//    public R saveCopy(Integer userId, @RequestBody String body){
//        //jiashu ju
//        userId=4;
//        R r = orderService.submit(userId,body);
//        return r;
//    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("更新指定id订单表信息")
    //@RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order) {
        orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除指定id订单表信息")
    //@RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Integer[] ids) {
        orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
